#! /usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) Huoty, All rights reserved
# Author: Huoty <sudohuoty@gmail.com>
# CreateTime: 2018-08-11 22:13:45

import sys
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

channel.exchange_declare(exchange='headers_logs', exchange_type='headers')

result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue

arguments = {
    "error": "error",
    "warning": "warning",
    "x-match": "any"  # 仅匹配一个日志级别即可
}
channel.queue_bind(exchange='headers_logs',
                   queue=queue_name,
                   routing_key='',
                   arguments=arguments)

print ' [*] Waiting for logs. To exit press CTRL+C'

def callback(ch, method, properties, body):
    print " [x] {headers}:{body}".format(headers=properties.headers,
                                         body=body)

channel.basic_consume(callback,
                      queue=queue_name,
                      no_ack=True)

channel.start_consuming()

